{-
    pHaskell Translator v0.1 
    Copyright (C) 2009  Ruben Marcelo Caro 

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-}
module Main where
import Paths_phaskell
import IO  
import System.Environment
import LambdaTypes
import Fase1
import Fase2
import GeneratrixRender
   
validName []     = []
validName (x:xs) |x=='.'    = '_':(validName xs)
				 |otherwise =  x:(validName xs)

makePragmaOnce filename =    "#ifndef " ++ (validName filename)
						  ++ "\n#define " ++ (validName filename)
						  ++ "\n#include \"prelude.h\"" ++ "\n\n"


openSecureFile name =   do 
                         openFile name ReadMode
openOutputFile name =   do 
                         openFile name WriteMode

--prelude = "data metalist = metalist a metalist | metaemptylist\n\n"
tanslateFile filename =do
                            preludeFullName  <- getDataFileName "doc/prelude.phs"
                            preludehFullName <- getDataFileName "c++_runtime/prelude.h"
                            putStrLn $ "Getting prelude from: "++preludeFullName
                            preludeHandle <- openSecureFile preludeFullName
                            preludehHandle <- openSecureFile preludehFullName
                            prelude  <- hGetContents preludeHandle
                            preludeh <- hGetContents preludehHandle
                            fromHandle  <- openSecureFile filename
                            -- Open the filenames, .h and .lambda (for debuging purpose)
                            lambdaOutHandle <- openOutputFile (filename++".lambda")
                            preludehOutHandle <- openOutputFile "prelude.h"
                            outHandle <- openOutputFile (filename++".h")
                            -- put the pragma once on the .h file
                            hPutStr outHandle (makePragmaOnce filename)	
                            -- get the phs program	 		
                            fileContent <- hGetContents fromHandle
                            -- print the file contene in .lambda and .h files
                            hPutStr lambdaOutHandle ("/*\npHaskell Code: \n\n"++fileContent++"\n*/\n\n\n")
                            hPutStr outHandle ("/*\npHaskell Code: \n\n"++fileContent++"\n*/\n\n\n")
                            -- start fase 1
                            fase1Result <- return (makeFase1 (prelude++"\n"++fileContent))
                            -- save the output on the .lambda file, and close the file
                            hPutStr lambdaOutHandle (lambdaPretyPrinterProgram fase1Result)
                            hClose lambdaOutHandle
                            -- save the fase 1 output as comment in .h file
                            hPutStr outHandle ("/*\nLambda Abstraction: \n\n"++(lambdaPretyPrinterProgram fase1Result)++"\n*/\n\n\n")
                            -- start the fase 1
                            fase2Result <-  return (makeFase2 fase1Result)
                            -- start the generatrix fase
                            generatrixes <- return (generatrixRender (prelude++"\n"++fileContent))
                            --putStr fileContent
                            putStr (lambdaPretyPrinterProgram fase1Result)
                            putStr fase2Result
                            -- save the fase 2 result and generatrix result in .h file
                            hPutStr outHandle fase2Result
                            hPutStr outHandle ("\n/* GENERATRIX SECTION */\n")
                            hPutStr outHandle generatrixes
                            hPutStr outHandle ("\n#endif\n")
                            hPutStr preludehOutHandle preludeh
                            -- close the opened file handlers
                            hClose fromHandle
                            hClose outHandle
                            hClose preludeHandle
                            hClose preludehOutHandle
                            putStr "\n\nDone."
									
--Hacerlo mejor con first 
processArgs (x:xs) = tanslateFile x
processArgs []     = tanslateFile "test.phs"
main  = do
        argv <- getArgs
        _ <- processArgs argv
        return ()
          
